const { executeQuery, executePagedQuery } = require('../utils/dataAccess');

exports.getDicList = async (req, res, next) => {
    let dict_type = req.body.dict_type || 'sys_normal_disable'
    // sys_dict_data
    let sql = `SELECT 
        create_by as 'createBy',
        create_time as 'createTime',
        css_class as 'cssClass',
        dict_code as 'dictCode',
        dict_sort as 'dictSort',
        dict_type as 'dictType',
        dict_label as 'dictLabel',
        dict_value as 'dictValue',
        is_default as 'isDefault',
        list_class as 'listClass',
        remark as 'remark',
        status as 'status',
        update_by as 'updateBy'
        FROM sys_dict_data where dict_type like '${dict_type}'`; // 假设这是你的SQL查询语句，用于获取字典列表数据。


    try {
        const result = await executeQuery(sql);
        res.json({
            code: 200,
            message: '查询成功',
            data: result, // 返回数据。
        })
    }
    catch (err) { 
        next(err)
    }
}

// 获取字典类型列表
exports.getDictTypeList = async (req, res, next) => {
    try {
        let sqlStatement = `SELECT 
            dict_id as 'dictId',
            dict_name as 'dictName',
            dict_type as 'dictType',
            create_by as 'createBy', 
            create_time as 'createTime', 
            update_by as 'updateBy', 
            update_time as 'updateTime', 
            remark as 'remark' 
            FROM 
            sys_dict_type
            `;
        
        if (req.body.dictName) {
            sqlStatement += ` where dict_name like '%${req.body.dictName}%'`
        }

        sqlStatement += ` order by dictId`
        const result = await executeQuery(sqlStatement);
        res.json({
            code: 200,
            message: '查询成功',
            data: result, // 返回数据。
        })
    }
    catch (err) {
        next(err)
    }
}

// 根据类型获取字典列表
exports.getDictListByType = async (req, res, next) => {
    try {
        if (!req.body.dictType) {
            throw new Error('字典类型不能为空')
        }

        let sqlStatement = `SELECT
            dict_code as 'dictCode',
            dict_sort as 'dictSort',
            dict_label as 'dictLabel',
            dict_value as 'dictValue',
            dict_type as 'dictType',
            status as 'status',
            remark as 'remark',
            create_by as 'createBy',
            create_time as 'createTime',
            update_by as 'updateBy',
            update_time as 'updateTime'
            FROM sys_dict_data where dict_type like '${req.body.dictType}'
        `;

        if (req.body.dictLabel) {
            sqlStatement += ` and dict_label like '%${req.body.dictLabel}%'`
        }

        if (req.body.status) {
           sqlStatement += ` and status = '${req.body.status}'`
        }

        let result = await executePagedQuery(sqlStatement, req.body.pageSize, req.body.pageNo, 'dictSort');
        res.json({
            code: 200,
            message: '查询成功',
            data: result,
        })

    }
    catch (err) {
        next(err)
    }
}

// 添加字典类型
exports.addDictType = async (req, res, next) => {
    try {
        let sqlStatement 
        if (req.body.dictId) {
            sqlStatement = `update sys_dict_type set dict_name='${req.body.dictName}', dict_type='${req.body.dictType}', remark='${req.body.remark}' where dict_id=${req.body.dictId}`
        } else {
            sqlStatement = `insert into sys_dict_type(dict_name, dict_type, remark) values('${req.body.dictName}', '${req.body.dictType}', '${req.body.remark}')`
        }

        let result = await executeQuery(sqlStatement);
        res.json({
            code: 200,
            message: '保存成功',
            data: result.rows, // 返回数据。
        })
    }
    catch (err) {
        next(err)
    }
}

// 删除字典类型
exports.deleteDictType = async (req, res, next) => {
    try {
        let sqlStatement = `delete from sys_dict_type where dict_id=${req.body.dictId}`

        let result = await executeQuery(sqlStatement);
        res.json({
            code: 200,
            message: '删除成功',
            data: result.rows, // 返回数据。
        })
    }
    catch (err) {
        next(err)
    }
}

// 新增编辑字典数据
exports.addDictData = async (req, res, next) => {
    try {
        let sqlStatement 
        if( req.body.dictCode) {
            sqlStatement = `update sys_dict_data set dict_sort=${req.body.dictSort}, dict_label='${req.body.dictLabel}', dict_value='${req.body.dictValue}', status='${req.body.status}', dict_Type='${req.body.dictType}', remark='${req.body.remark}' where dict_code=${req.body.dictCode}`
            let checkSql = `SELECT TOP 1 1 FROM sys_dict_data 
                             WHERE dict_value='${req.body.dictValue}' 
                             AND dict_type='${req.body.dictType}' 
                             AND dict_code!=${req.body.dictCode}`;
            let checkResult = await executeQuery(checkSql);
            if (checkResult.rows.length > 0) {
                throw new Error('字典值已存在');
            }
            
        } else {
            sqlStatement = `insert into sys_dict_data(dict_sort, dict_label, dict_value, dict_type, status, remark) values(${req.body.dictSort}, '${req.body.dictLabel}', '${req.body.dictValue}', '${req.body.dictType}', '${req.body.status}', '${req.body.remark}')`
            let checkSql = `SELECT TOP 1 1 FROM sys_dict_data WHERE dict_value='${req.body.dictValue}' AND dict_type='${req.body.dictType}'`;
            let checkResult = await executeQuery(checkSql);
            if (checkResult.rows.length > 0) {
                throw new Error('字典值已存在');
            }
        }

        let result = await executeQuery(sqlStatement);
        res.json({
            code: 200,
            message: '保存成功',
            data: result.rows, // 返回数据。
        })
    }
    catch (err) {
        next(err)
    }
}

// 删除字典数据
exports.deleteDictData = async (req, res, next) => {
    try {
        let sqlStatement = `delete from sys_dict_data where dict_code=${req.body.dictCode}`

        let result = await executeQuery(sqlStatement);
        res.json({
            code: 200,
            message: '删除成功',
            data: result.rows, // 返回数据。
        })
    }
    catch (err) {
        next(err)
    }
}

// 字典数据状态修改
exports.updateDictDataStatus = async (req, res, next) => {
    try {
        let sqlStatement = `update sys_dict_data set status='${req.body.status}' where dict_code=${req.body.dictCode}`

        let result = await executeQuery(sqlStatement);
        res.json({
            code: 200,
            message: '修改成功',
            data: result.rows, // 返回数据。
        })
    }
    catch (err) {
        next(err)
    }
}